package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.EmsGridRecord;
import com.econ.powercloud.ems.dal.mapper.EmsGridRecordMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

@Repository
public class EmsGridRecordDAO extends ServiceImpl<EmsGridRecordMapper, EmsGridRecord> {

    public Page<EmsGridRecord> selectPage(String energyStationId, Integer pageNo, Integer pageSize) {
        Page<EmsGridRecord> page = new Page(pageNo, pageSize);
        LambdaQueryWrapper<EmsGridRecord> wrapper = Wrappers.<EmsGridRecord>lambdaQuery()
                .eq(StringUtils.isNotEmpty(energyStationId), EmsGridRecord::getEnergyStationId, energyStationId)
                .orderByDesc(EmsGridRecord::getCreateTime);
        return this.baseMapper.selectPage(page, wrapper);
    }

    public EmsGridRecord selectLeast(String energyStationId){
        LambdaQueryWrapper<EmsGridRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StringUtils.isNotEmpty(energyStationId), EmsGridRecord::getEnergyStationId, energyStationId);
        wrapper.orderByDesc(EmsGridRecord::getCreateTime);
        wrapper.last("limit 1");
        return this.baseMapper.selectOne(wrapper);
    }

    public Integer selectCountByStationIdAndStatus(String energyStationId, Integer status) {
        LambdaQueryWrapper<EmsGridRecord> wrapper = Wrappers.<EmsGridRecord>lambdaQuery()
                .eq(EmsGridRecord::getEnergyStationId, energyStationId).eq(EmsGridRecord::getStatus, status);
        return this.baseMapper.selectCount(wrapper);
    }
}
